package com.gaoboy.springbootgoods.controller;

import com.gaoboy.springbootgoods.entity.Order;
import com.gaoboy.springbootgoods.service.OrderService;
import com.gaoboy.springbootgoods.utils.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class OrderController {

    @Autowired
    private OrderService orderService;

    @RequestMapping("goods/saveOrder")
    public String saveOrder(int[] id, HttpServletRequest request){
        HttpSession session = request.getSession();
        Integer userId = (Integer) session.getAttribute("userId");
        HttpUtil.threadLocal.set(userId);

        String orderNum = orderService.saveOrder(id);
        request.setAttribute("orderNum",orderNum);
        return "success";
    }

    @RequestMapping("order/queryByUserId")
    public String queryByUserId(HttpServletRequest request){
        HttpSession session = request.getSession();
        Integer userId = (Integer)session.getAttribute("userId");
        List<Order> orderList = orderService.queryByUserId(userId);
        request.setAttribute("orderList",orderList);
        return "orderlist";
    }
}
